package com.wifi.user.dao;


import com.wifi.unti.DBHelper;
import com.wifi.user.entity.User;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImp implements UserDao {
    @Override
    public Object login(User user, String name, String pwd) throws SQLException {
        String sql="SELECT * FROM `shenzhou`.`tb_admin` where name=? and password=?";
        return DBHelper.select(sql,user,name,pwd);
    }

    @Override
    public int updateLastLoginTime(Integer id) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_admin` SET lastLoginTime=now() WHERE `id` = ?";
        return DBHelper.update(sql,id);
    }

    @Override
    public Object selectUserById(String id) throws SQLException {
        String sql="SELECT * FROM `shenzhou`.`tb_admin` where id=?";
        return DBHelper.select(sql,new User(),Integer.valueOf(id));
    }

    @Override
    public Integer updatePwd(String pwd, int id) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_admin` SET `password` = ? WHERE `id` = ?";
        return DBHelper.update(sql,pwd,id);
    }

    @Override
    public List<Object> selectUser() throws SQLException {
        String sql="SELECT * FROM `shenzhou`.`tb_admin` ";
        return DBHelper.selectAll(sql,new User());
    }

    @Override
    public Long insertUser(User user) throws SQLException {
        String sql="INSERT INTO `shenzhou`.`tb_admin`(`name`, `password`, `role`, `createTime`, `vlid`) VALUES (?, ?, ?, now(), ?)";
        return DBHelper.insert(sql,user.getName(),user.getPassword(),user.getRole(),user.getVlid());
    }

    @Override
    public Integer updateStatus(Integer idi,Integer vlid) throws SQLException {
        String sql;
        if (vlid==0){
          sql="UPDATE `shenzhou`.`tb_admin` SET `vlid` = 1 WHERE `id` = ?";
        }else if (vlid==1){
            sql="UPDATE `shenzhou`.`tb_admin` SET `vlid` = 0 WHERE `id` = ?";
        }else{
            return 0;
        }
        return DBHelper.update(sql,idi);
    }

    @Override
    public Integer updateDelu(Integer idi) throws SQLException {
        String sql="DELETE FROM `shenzhou`.`tb_admin` WHERE `id` = ?";
        return DBHelper.update(sql,idi);
    }

    @Override
    public Object selectByName(User user, String username) throws SQLException {
        String sql="SELECT * FROM `shenzhou`.`tb_admin` where name=?";
        return DBHelper.select(sql,new User(),username);
    }

    @Override
    public Integer updateUser(User user) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_admin` SET `name` = ?, `role` = ?, `vlid` =? WHERE `id` = ?";
        return DBHelper.update(sql,user.getName(),user.getRole(),user.getVlid(),user.getId());
    }

    @Override
    public Integer updatePwd(String id, String num, String pwdMD5) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_admin` SET `password` =? WHERE `id` = ? and name = ?";
        return DBHelper.update(sql,pwdMD5,Integer.valueOf(id),num);
    }


}
